﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;
using Spinning.Spinning.Unitity;
using Microsoft.Xna.Framework;
using Microsoft.Devices.Sensors;
using Spinning.Spinning.Entity.Items;

namespace Spinning.Spinning.Entity.Scenes.SinglePlayer
{
    class SinglePlayerStage2BossScene : SinglePlayerScene
    {
        List<AddTimeItem> AddTimeItemList = new List<AddTimeItem>();
        EnemyBoss Boss;
        WindBladeShoter wshoter;

        public override void OnLoad()
        {
            base.OnLoad();
            Sprite backGroundSprite = new Sprite();
            backGroundSprite.textureName = "singleStage2Boss/k_boss_bg";
            backGroundSprite.ZIndex = Constants.background;
            backGroundSprite.OnLoad();
            this.Attach(backGroundSprite);

            Player.LifeTime = Game1.stageManager.LifeTimeEarnByUser * 5;

            Boss = new EnemyBoss();
            Boss.textureName = "spinning";
            Boss.OnLoad();
            Boss.SetAnimation(Constants.playerAnimationFrame);
            Boss.Style = 3;
            Boss.Enemy_Type = EnemyBoss.BossType.Wind;
            Boss.LifeTime = 205;  //魔王初始秒數
            Boss.OnCreatBody(World, PhysicConnectManager);
            Boss.BodyPosiotion = new Vector2(240, 200);
            //Boss.ColdDownTime = 8;
            //Boss.EffectTime = 3;

            Boss.OnBeginSpecialAttackEvent += BeginAttack;
            Boss.OnEndSpecialAttackEvent += EndAttack;
            this.Attach(Boss);

            Bound b1 = new Bound();
            b1.Size = new Vector2(Constants.Window_Width, 20);
            b1.OnCreatBound(World);
            b1.Position = new Vector2(Constants.Window_Width / 2, 800);//下界
            Attach(b1);

            Bound b2 = new Bound();
            b2.Size = new Vector2(Constants.Window_Width, 20);
            b2.OnCreatBound(World);
            b2.Position = new Vector2(Constants.Window_Width / 2, 0);//上界
            Attach(b2);

            for (int i = 0; i < 18; i++)
            {
                AddTimeItem accItem = new AddTimeItem();
                accItem.textureName = "singleStage1/accelerate";
                accItem.OnLoad();
                accItem.ItemLevel = 5;
                accItem.SetAnimation(new Point(1, 1));
                accItem.OnCreatBody(World, PhysicConnectManager);
                accItem.BodyPosiotion = new Vector2(-200, 200);
                this.Attach(accItem);
                AddTimeItemList.Add(accItem);
            }

            isCameraMove = false;

            wshoter = new WindBladeShoter(this);
            wshoter.OnLoad();
            Attach(wshoter);

            SortChildren();
        }

        const int nextAppearTime = 6, appearTime = 3;
        int _gameTime, counter, itemCounter;
        protected override void UpdateSelf(GameTime gameTime)
        {
            base.UpdateSelf(gameTime);
            updateBossMotion(gameTime);
        }

        private void updateBossMotion(GameTime gameTime)
        {
            int delta = gameTime.ElapsedGameTime.Milliseconds;
            _gameTime += delta;
            if (_gameTime / 1000 >= 1)
            {
                _gameTime = _gameTime % 1000;
                counter++;
                if (counter == appearTime)
                {
                    if (itemCounter > 0 && itemCounter - 1 < AddTimeItemList.Count) AddTimeItemList[itemCounter - 1].BodyPosiotion = new Vector2(-200, 200);
                }
                if (counter == nextAppearTime)
                {
                    counter = 0;
                    if (itemCounter < AddTimeItemList.Count) AddTimeItemList[itemCounter].BodyPosiotion = randomPosition();
                    itemCounter++;
                }
            }
            te.Value += "\nBossLifeTime:" + Boss.LifeTime + "\n" + wshoter.blades.Count;
        }

        int attackCounter;

        void BeginAttack()
        {
            attackCounter = 0;
            this.UpdateHandlerListener += bossAttack;
        }

        void EndAttack()
        {
            this.UpdateHandlerListener -= bossAttack;
        }

        void bossAttack(GameTime gameTime)
        {
            int dt = 5;
            int time = 3;
            for (int i = 0; i < time; i++) {
                if (attackCounter ==  dt * i)
                {
                    wshoter.Shot(Boss.Position, 500);
                }
            }
                
            attackCounter += 1;
        }

        Vector2 randomPosition()
        {
            Vector2 v = new Vector2();
            Random random = new Random();
            int randomNumber = random.Next(1, 10);
            switch (randomNumber)
            {
                case 1:
                    v = new Vector2(240, 100);
                    break;
                case 2:
                    v = new Vector2(240, 700);
                    break;
                case 3:
                    v = new Vector2(60, 400);
                    break;
                case 4:
                    v = new Vector2(420, 400);
                    break;
                case 5:
                    v = new Vector2(240, 400);
                    break;
                case 6:
                    v = new Vector2(60, 100);
                    break;
                case 7:
                    v = new Vector2(420, 100);
                    break;
                case 8:
                    v = new Vector2(60, 700);
                    break;
                case 9:
                    v = new Vector2(420, 700);
                    break;
            }
            return v;
        }

        public override Scene Clone()
        {
            Scene scene = new SinglePlayerStage2BossScene();
            scene.Size = new Vector2(Constants.Window_Width, Constants.Window_Height);
            scene.OnLoad();
            scene.SortChildren();
            return scene;
        }
    }
}
